package com.example.shgt.adapter;
import com.bumptech.glide.Glide; // 确保导入 Glide

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.shgt.DetailsActivity;
import com.example.shgt.R;
import com.example.shgt.TransactionRecordsActivity;
import com.example.shgt.entity.GoodsData;
import com.example.shgt.entity.ProductInfo;
import com.example.shgt.entity.Record;
import com.example.shgt.fragment.HomeFragment;

import java.util.ArrayList;
import java.util.List;

public class RightListAdapter extends RecyclerView.Adapter<RightListAdapter.MyHolder> {

//    private List<ProductInfo> mProductInfos = new ArrayList<>();
    private List<Record> records;
    private Context mContext;
    private RightListAdapter mListAdapter;
    private List<String> imageUrlList;


    public RightListAdapter(List<Record> records) {

        this.records = records;

    }




    static class MyHolder extends RecyclerView.ViewHolder {
        ImageView gooodsImage;
        TextView content;
        TextView username;
        TextView product_price;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            //找到控件

            gooodsImage = itemView.findViewById(R.id.product_img);
            username = itemView.findViewById(R.id.username);
            content = itemView.findViewById(R.id.content);
            product_price = itemView.findViewById(R.id.price);
        }
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 加载布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.right_list_item, parent, false);
        mContext = parent.getContext(); // 保存上下文
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        // 绑定数据
        Record record = records.get(position);

// 检查 URL 列表，获取第一个 URL
        List<String> imageUrlList = record.getImageUrlList();
        String imageUrlToLoad = null;

        // 确保 URL 列表不为空，取第一个 URL
        if (imageUrlList != null && !imageUrlList.isEmpty()) {
            imageUrlToLoad = imageUrlList.get(0); // 取第一个 URL
        }

        // 加载商品图片
        Glide.with(holder.itemView.getContext())
                .load(imageUrlToLoad) // 使用有效的图片 URL
                .into(holder.gooodsImage);

        holder.content.setText(record.getContent());
        holder.username.setText(record.getUsername());
        holder.product_price.setText(record.getPrice() + "");

        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (mRightListOnItemClickListener != null) {
                mRightListOnItemClickListener.onItemClick(record.getId()); // 传递商品 ID
            }
        });




    }
    @Override
    public int getItemCount() {
        return records.size();
    }

    private RightListOnItemClickListener mRightListOnItemClickListener;

//    public void setRightListOnItemClickListener(RightListOnClickItemLitener mRightListOnClickItemLitener) {
//        this.mRightListOnClickItemLitener = mRightListOnClickItemLitener;
//    }


    // 设置点击事件监听器
    public void setRightListOnItemClickListener(RightListOnItemClickListener listener) {
        this.mRightListOnItemClickListener = listener; // 设置监听器
    }

    //点击事件，接口回调
    public interface RightListOnItemClickListener{
        void onItemClick(long ProductId);
    }



    // 设置数据列表
    public void setListData(List<Record> list) {
        this.records = list;
        // 一定要刷新RecyclerView
        notifyDataSetChanged();
    }

}

